#!/usr/bin/env python

import wsgiref.handlers
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.api.labs import taskqueue
from schema import *

import logging

class RankInitializer(webapp.RequestHandler):
  def post(self):
    computationid = long(self.request.get("computationid"))
    logging.debug("I'll count nodes for computation %i." % computationid)
    docid = checksum_from_key_name(self.request.get("docid"))
    
    logging.debug("Now I'll initialize a rank.")
    ranks = DocumentRank.all().filter("computationid =", computationid)
    linked_rank = ranks.filter("docid =", db.ByteString(docid)).get()
    if linked_rank is None:
      linked_rank = DocumentRank(computationid=computationid, docid=docid)
      linked_rank.put()


def main():
  logging.getLogger().setLevel(logging.DEBUG)
  wsgiref.handlers.CGIHandler().run(webapp.WSGIApplication([
    ('/init-rank', RankInitializer),
  ]))


if __name__ == '__main__':
  main()
